;
(function() {

	var role = require("./role");
	var BusCon = require("../../core/bus/_con");
	var UserCon = require("../../core/user/_con");
	var GoodsCon = require("../../core/goods/_con");
	var AchCon = require("../../core/ach/_con");
	var db = require("../../core/db/index");
	var config = require("../../core/_include/config");
	var ResponObj = require("../_include/responObj");
	module.exports = {
		prefix: "/bus/extend/bina",
		get: {
			"/goods": function() {
				var bus = role.getBus(this);
				this.body = ResponObj("json", bus.getBinaGoodsList());
			},
			"/user": function() {
				var bus = role.getBus(this);
				this.body = ResponObj("json", bus.getBinaUser());
			},
			"/config": function(argument) {
				var bus = role.getBus(this);
				this.body = ResponObj("json", bus.getBinaBuff());
			},
			"/user_list/byId/:bina_id": function() {
				var bus = role.getBus(this);
				this.body = ResponObj("json", bus.getUserListByBinaId(this.params.bina_id));
			},
			"/user_map/byId/:bina_id": function() {
				var bus = role.getBus(this);
				this.body = ResponObj("json", bus.getUserMapByBinaId(this.params.bina_id));
			},
			"/bina_list/byId/:user_id": function() {
				var bus = role.getBus(this);
				this.body = ResponObj("json", bus.getBinaListByUserId(this.params.user_id));
			},
		},
		post: {
			"/add_user/:bina_id": function() {
				var bus = role.getBus(this);
				var bina_id = this.params.bina_id;
				var body = this.request.body;
				var user_id = body.user_id;
				var recommender_id = body.recommender_id;
				var user_maybe_info = body.user_maybe_info; //模糊搜索用户
				if (!user_id) {
					if (!user_maybe_info) {
						throwCusError("参数错误")
					} else {
						var user = UserCon.search(user_maybe_info);
						if (!user) {
							throwCusError("找不到指定用户")
						}
						user_id = user._id;
					}
				}
				bus.addUserToBina(bina_id, user_id, recommender_id);
				if (body.return_type === "bina_list") {
					this.body = ResponObj("json", bus.getBinaListByUserId(user_id));
				} else {
					this.body = ResponObj("json", bus.getUserListByBinaId(bina_id));
				}
			},
			"/add_user_to_bina_list": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var bina_id_list = body.bina_id_list;
				if (!(bina_id_list instanceof Array)) {
					throwCusError("参数错误")
				}
				var user_id = body.user_id;
				var recommender_id = body.recommender_id;
				var user_maybe_info = body.user_maybe_info; //模糊搜索用户
				if (!user_id) {
					if (!user_maybe_info) {
						throwCusError("参数错误")
					} else {
						var user = UserCon.search(user_maybe_info);
						if (!user) {
							throwCusError("找不到指定用户")
						}
						user_id = user._id;
					}
				}
				bina_id_list.forEach(function(bina_id) {
					try {
						bus.addUserToBina(bina_id, user_id);
					} catch (e) {
						console.error(e);
						//批量处理时，不抛出错误
						//把能处理的处理了就好
					}
				});
				if (body.return_type === "bina_list") {
					this.body = ResponObj("json", bus.getBinaListByUserId(user_id));
				} else {
					this.body = ResponObj("json", bus.getBinaUser());
				}
			},
			"/add_user_to_forever_bina_user_list": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var user_id = body.user_id;
				if (!user_id) {
					throwCusError("参数错误")
				}
				this.body = ResponObj("json", bus.addUserToForeverBinaUserList(user_id));
			},
			"/add_user_to_senior_bina_user_list": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var user_id = body.user_id;
				if (!user_id) {
					throwCusError("参数错误")
				}
				this.body = ResponObj("json", bus.addUserToSeniorBinaUserList(user_id));
			},
		},
		put: {
			"/goods": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var goods_id_list = body.goods_id_list || [];

				bus.setBinaGoodsList(goods_id_list);
				this.body = ResponObj("json", bus.getBinaGoodsList());
			},
			"/申请兑换中_to_商家已付款": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var user_id = body.user_id;
				if (!user_id) {
					throwCusError("参数错误")
				}
				bus.bina_申请兑换中_to_商家已付款(user_id);
				var user_bina_info = bus.getBinaUserById(user_id);
				this.body = ResponObj("json", user_bina_info);
			},
			"/config": function() {
				var bus = role.getBus(this);
				var config = this.request.body;
				if (config.first == undefined || config.second === undefined) {
					throwCusError("参数错误")
				}
				var data_bina_buff = bus.setBinaBuff(config);
				this.body = ResponObj("json", data_bina_buff);
			},
			"/goods_custom_bina/:goods_id": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var goods_id = this.params.goods_id;
				this.body = ResponObj("json", bus.setGoodsBinaBuff(goods_id, body));
			},
			"/lock_user": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var user_id = body.user_id;
				var bina_id = body.bina_id;
				if (!(user_id && bina_id)) {
					throwCusError("参数错误")
				}
				bus.lockUserFromBina(bina_id, user_id);
				if (body.return_type === "bina_list") {
					this.body = ResponObj("json", bus.getBinaListByUserId(user_id));
				} else {
					this.body = ResponObj("json", bus.getUserListByBinaId(bina_id));
				}
			},
			"/unlock_user": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var user_id = body.user_id;
				var bina_id = body.bina_id;
				if (!(user_id && bina_id)) {
					throwCusError("参数错误")
				}
				bus.unlockUserFromBina(bina_id, user_id);
				if (body.return_type === "bina_list") {
					this.body = ResponObj("json", bus.getBinaListByUserId(user_id));
				} else {
					this.body = ResponObj("json", bus.getUserListByBinaId(bina_id));
				}
			},
		},
		"delete": {
			"/remove_goods_custom_bina/:goods_id": function() {
				var bus = role.getBus(this);
				var goods_id = this.params.goods_id;
				console.log(goods_id);
				this.body = ResponObj("json", bus.removeGoodsBinaBuff(goods_id));
			},
			"/remove_user_form_forever_bina_user_list": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var user_id = body.user_id;
				if (!user_id) {
					throwCusError("参数错误")
				}
				this.body = ResponObj("json", bus.removeUserFromForeverBinaUserList(user_id));
			},
			"/remove_user_form_senior_bina_user_list": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var user_id = body.user_id;
				if (!user_id) {
					throwCusError("参数错误")
				}
				this.body = ResponObj("json", bus.removeUserFromSeniorBinaUserList(user_id));
			},
		}
	}
}());